package com.wwwarehouse.contract.orders;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import contract.wwwarehouse.com.contract.R;


/**
 * author ：minifly
 * date: 2016/12/28
 * time: 14:29
 * desc:
 */
public class BuyerAnimatorUtils {
    /**
     * 商品添加到购物车动画
     * @param fatherRel
     */
    public static void addCart(Context context,final ViewGroup fatherRel, View startView, View endView) {
        /**
         * 贝塞尔曲线中间过程的点的坐标
         */
        final float[] mCurrentPosition = new float[2];
        final PathMeasure mPathMeasure;
        final ImageView goods = new ImageView(context);
        goods.setImageResource(R.drawable.add_goods);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(36, 36);
        fatherRel.addView(goods, params);

        int[] parentLocation = new int[2];
        fatherRel.getLocationInWindow(parentLocation);

        int startLoc[] = new int[2];
        startView.getLocationInWindow(startLoc);

        int endLoc[] = new int[2];
        endView.getLocationInWindow(endLoc);
        float startX = startLoc[0] - parentLocation[0];
        float startY = startLoc[1] - parentLocation[1];

        float toX = endLoc[0] - parentLocation[0] + endView.getWidth() / 5;
        float toY = endLoc[1] - parentLocation[1];

        Path path = new Path();
        path.moveTo(startX, startY);
        path.quadTo((startX + toX) / 2, startY, toX, toY);
        mPathMeasure = new PathMeasure(path, false);
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mPathMeasure.getLength());
        valueAnimator.setDuration(500);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (Float) animation.getAnimatedValue();
                mPathMeasure.getPosTan(value, mCurrentPosition, null);//mCurrentPosition此时就是中间距离点的坐标值
                goods.setTranslationX(mCurrentPosition[0]);
                goods.setTranslationY(mCurrentPosition[1]);
            }
        });
        valueAnimator.start();

        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                fatherRel.removeView(goods);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

}
